package com.ljkj.cordial.chat.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.ljkj.cordial.chat.BuildConfig;
import com.ljkj.cordial.chat.R;
import com.ljkj.cordial.chat.util.ObserverThread;
import com.ljkj.cordial.chat.util.SMFrameCallback;
import com.ljkj.cordial.chat.widget.NetProgressBarDialog;
import com.nic.view.activity.VBaseActivity;
import com.nic.view.util.JToast;

public abstract class BaseActivity extends VBaseActivity {
    private NetProgressBarDialog netProgressBarDialog;
    private boolean isShowDefNetDialog = true;
    private long lastClickTime;
    private boolean allowContinueClick;

    public void setAllowContinueClick(boolean allowContinueClick) {
        this.allowContinueClick = allowContinueClick;
    }

    public void setShowDefNetDialog(boolean showDefNetDailog) {
        isShowDefNetDialog = showDefNetDailog;
    }

    public boolean isShowDefNetDialog() {
        return isShowDefNetDialog;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ChatApp.getInstance().addActivity(this);
        if (BuildConfig.DEBUG) {
            SMFrameCallback.getInstance().start();
        }
    }

    public static View getContentView(Activity context) {
        return ((ViewGroup) context.findViewById(android.R.id.content)).getChildAt(0);
    }

    @Override
    protected void onDestroy() {
        ChatApp.getInstance().removeActivity(this);
        setShowDefNetDialog(true);
        ObserverThread.create().shutdownThread();
        super.onDestroy();
    }

    public void showNet(boolean isShowDefNetDialog) {
        if (isFinishing()) {
            return;
        }
        if (isShowDefNetDialog) {
            if (netProgressBarDialog == null) {
                netProgressBarDialog = new NetProgressBarDialog(this);
                netProgressBarDialog.setDes("加载中...");
            } else {
                netProgressBarDialog.setDes("加载中...");
            }
            netProgressBarDialog.show();
        }
    }

    public void showNet(String prompt, boolean flag) {
        if (isFinishing()) {
            return;
        }
        if (isShowDefNetDialog()) {
            if (netProgressBarDialog == null) {
                netProgressBarDialog = new NetProgressBarDialog(this, flag);
                netProgressBarDialog.setDes(prompt);
            } else {
                netProgressBarDialog.setDes(prompt);
            }
            netProgressBarDialog.show();
        }
    }

    public void dismissNet(boolean isShowDefNetDialog) {
        if (isFinishing()) {
            return;
        }
        if (isShowDefNetDialog && null != netProgressBarDialog) {
            netProgressBarDialog.dismiss();
            netProgressBarDialog = null;
        }
    }

    /**
     * 退出
     */
    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN
                && event.getRepeatCount() == 0) {
            dismissNet(true);
        }
        return super.dispatchKeyEvent(event);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return super.onTouchEvent(event);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (!allowContinueClick && ev.getAction() == MotionEvent.ACTION_DOWN) {
            long value = System.currentTimeMillis() - lastClickTime;
            if (value < 500) {
                return true;
            }
            lastClickTime = System.currentTimeMillis();
        }
        return super.dispatchTouchEvent(ev);
    }

    public void showToastSHORT(String text) {
        JToast.show(this, text, Toast.LENGTH_SHORT);
    }

    protected void openActivity(Class<?> activity) {
        Intent intent = new Intent(this, activity);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }


    @Override
    protected void attachBaseContext(Context newBase) {
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N) {
            final Resources res = newBase.getResources();
            final Configuration config = res.getConfiguration();
            config.fontScale = ChatApp.getInstance().getFontSize();//1 设置正常字体大小的倍数
            final Context newContext = newBase.createConfigurationContext(config);
            super.attachBaseContext(newContext);
        } else {
            super.attachBaseContext(newBase);
        }
    }

    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.N) {
            Configuration config = res.getConfiguration();
            config.fontScale = ChatApp.getInstance().getFontSize();//1 设置正常字体大小的倍数
            res.updateConfiguration(config, res.getDisplayMetrics());
        }
        return res;
    }

    /**
     * 设置按钮提交状态
     *
     * @param textView
     * @param isEnable
     */
    public void setButtonSummitState(TextView textView, boolean isEnable) {
        if (isEnable) {
            textView.setEnabled(true);
            textView.setTextAppearance(mContext, R.style.ButtonGreenStyle);
            textView.setBackgroundResource(R.drawable.btn_green);
        } else {
            textView.setEnabled(false);
            textView.setTextAppearance(mContext, R.style.ButtonGrayStyle);
            textView.setBackgroundResource(R.drawable.btn_gray);
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
    }

}
